package com.yonyou.pmclouds.offline;

import com.yonyou.pmclouds.offline.aspect.Offline;
import com.yonyou.pmclouds.offline.rmiitf.IOffLineDataDownloadService;
import com.yonyou.pmclouds.offline.rmiitf.IOffLineDataQueryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.AnnotationUtils;

@Configuration
public class OffLineServiceBeanConfig implements BeanPostProcessor{

    @Autowired
    private IOffLineDataDownloadService offLineDataDownloadService;

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if(bean instanceof IOffLineDataQueryService){
            Offline offline = AnnotationUtils.findAnnotation(bean.getClass(), Offline.class);
            if(offline != null && StringUtils.isNotEmpty(offline.dataFrom())){
                offLineDataDownloadService.addService(offline.dataFrom(), (IOffLineDataQueryService) bean);
            }

        }
        return bean;
    }
}
